//
//  WCNCareRecordTCell.m
//  WCN
//
//  Created by 杨鑫 on 15/6/17.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNCareRecordTCell.h"
//
#import "WCImageManager.h"

@interface WCNCareRecordTCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;
@property (strong, nonatomic) NSArray *localImages;

@end

@implementation WCNCareRecordTCell

- (void)awakeFromNib
{
    self.nurseAvatar.layer.cornerRadius = 22;
    self.nurseAvatar.clipsToBounds = YES;
//    UILongPressGestureRecognizer *gestureLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(gestureLongPress:)];
//    gestureLongPress.minimumPressDuration =2;
//    [self.contentView addGestureRecognizer:gestureLongPress];

}

#pragma mark - Setter

- (void)setImgs:(NSArray *)imgs
{
    //只用 3张图片
    NSInteger count ;
    if (imgs.count>3) {
        count = 3;
        _imgs = [NSArray arrayWithObjects:imgs[0],imgs[1],imgs[2], nil];
    }else{
        _imgs = imgs;
        count =imgs.count;
    
    }
    self.imageView1.image = nil;
    self.imageView2.image = nil;
    self.imageView3.image = nil;
    
    for (int i=1; i<=count; i++) {
        UIImageView *imageView = [self valueForKey:[NSString stringWithFormat:@"imageView%d", i]];
        NSString *imgUrl = imgs[i-1];
        UIImage *image = [[WCImageManager sharedMg] image:imgUrl type:WCImageTypePrivateImage size:WCImageSizeSmall cache:YES];
        if (image) {
            imageView.image = image;
        } else {
            [[WCImageManager sharedMg] downloadImage:imgUrl type:WCImageTypePrivateImage size:WCImageSizeSmall cache:YES success:^(UIImage *image, NSString *stringId){
                if ([stringId isEqualToString:imgs[i-1]]) {
                    imageView.image = image;
                }
            } failure:nil];
        }
    }
}

- (void)setImgsFromLocal:(NSArray *)imgs
{
    _localImages = [imgs copy];
    //只用 3张图片
    NSInteger count ;
    if (imgs.count>3) {
        count = 3;
        _imgs = [NSArray arrayWithObjects:imgs[0],imgs[1],imgs[2], nil];
    }else{
        _imgs = imgs;
        count =imgs.count;
        
    }
    
    self.imageView1.image = nil;
    self.imageView2.image = nil;
    self.imageView3.image = nil;
    _images = [NSMutableArray array];
   
    for (int i = 0; i < self.localImages.count; i++) {
    
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                
                UIImage *image = [[WCImageManager sharedMg] imageForUpload:self.localImages[i]];
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (image) {
//                        if (i < count) {
//                            
//                            UIImageView *imageView = [self valueForKey:[NSString stringWithFormat:@"imageView%d", i]];
//                            imageView.image = image;
//                        }
                        [self.images addObject:image];
                    }
                });
            });
            
        }
        
    
    for (int i=1; i<=count; i++) {
        UIImageView *imageView = [self valueForKey:[NSString stringWithFormat:@"imageView%d", i]];
        imageView.image = [[WCImageManager sharedMg] imageForUpload:imgs[i-1]];
    }
}

#pragma mark - 长按显示删除键

#pragma mark - Nib Action

- (IBAction)commentap:(id)sender {
    self.showComment(self.commentStr);
}

@end
